/**
 * org.vz.product.vserv.pf.impl
 * @since : jdk 1.8
 */
package org.vz.product.vserv.pf.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.vz.product.vserv.pf.domain.entity.DruingTimeEntity;
import org.vz.product.vserv.pf.domain.entity.RecivedOvershootCount;
import org.vz.product.vserv.pf.domain.result.ReceivedSerOverviewCountResult;
import org.vz.product.vserv.pf.service.ReceivedSerCountService;
import org.vz.product.vserv.pf.service.ReceivedSerExceptionAndWaitCountService;
import org.vz.product.vserv.pf.service.ReceivedSerOvershootCountService;
import org.vz.product.vserv.pf.service.ReceivedSerOverviewService;

/**
 * @author morik is Yongxu.Wang 获取时间段服务概述
 * @CreateTime 2017年12月06日 10:16:03
 * @version 1.0.0
 * @description
 */
@Service
public class ReceivedSerOverviewServiceImpl implements
		ReceivedSerOverviewService {
	@Autowired
	ReceivedSerCountService receivedSerCountService;
	@Autowired
	ReceivedSerExceptionAndWaitCountService receivedSerExceptionAndWaitCountService;
	@Autowired
	ReceivedSerOvershootCountService receivedSerOvershootCountService;

	@Override
	public ReceivedSerOverviewCountResult receivedSerOverviewService(
			String startTime, String endTime, long overShoot) {
		ReceivedSerOverviewCountResult rsocr = new ReceivedSerOverviewCountResult();
		rsocr.data.useCount = receivedSerCountService.getSerCount(startTime,
				endTime).data.count;
		rsocr.data.exceptionCount = receivedSerExceptionAndWaitCountService
				.receivedSerExceptionAndWaitCountService(startTime, endTime).data.count;
		DruingTimeEntity dt = new DruingTimeEntity();
		dt.startTime = startTime;
		dt.endTime = endTime;
		dt = setTime(dt);
		RecivedOvershootCount data = receivedSerOvershootCountService
				.receivedSerOvershootCountService(dt.startTime, dt.endTime,
						overShoot).data;
		rsocr.data.count = data.count;
		rsocr.data.tatal = data.tatal;
		rsocr.data.persiont = data.persiont;
		rsocr.msg = "查询成功!!!";
		rsocr.status = 1;
		return rsocr;
	}

	/**
	 * 定义时间
	 * 
	 * @param druingTimeEntity
	 * @return
	 */
	private DruingTimeEntity setTime(DruingTimeEntity druingTimeEntity) {
		DruingTimeEntity dt = new DruingTimeEntity();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long druingTime = -1l;
		long endt = -1l;
		try {
			endt = sdf.parse(druingTimeEntity.startTime).getTime();
			druingTime = sdf.parse(druingTimeEntity.endTime).getTime() - endt;
			druingTime /= 6;
		} catch (ParseException e) {
			e.printStackTrace();
			Calendar c = Calendar.getInstance();
			if (endt == -1l) { // 结束时间无效
				dt.startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60
						* 1);
				dt.endTime = sdf.format(c.getTimeInMillis());
			} else if (druingTime == -1) { // 开始时间无效
				dt.startTime = sdf.format(endt - 1000 * 60 * 60 * 1);
				dt.endTime = druingTimeEntity.endTime;
			}
			return dt;
		}
		dt.startTime = sdf.format(endt - druingTime);
		dt.endTime = druingTimeEntity.endTime;
		return dt;
	}
}
